<template>
  <div class="searchForm">
    <el-form
      ref="searchForm"
      :rules="rules"
      :model="formInline"
      label-width="120px"

    >
      <el-form-item :label="$t('MaintenancePlanName')" prop="pmId">
        <el-select v-model="formInline.pmId" @change="getEqpList">
          <el-option
            v-for="item of PMFormList"
            :key="item.id"
            :label="item.name"
            :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('Checklist')" prop="formName">
        <el-input v-model="formInline.formName"/>
      </el-form-item>
      <el-form-item :label="$t('eqpId')" prop="eqpId">
        <el-select v-model="formInline.eqpId" >
          <el-option
            v-for="item of eqpList"
            :key="item"
            :label="item"
            :value="item" />
        </el-select>
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="24" :offset="19">
        <el-button

          type="primary"
          @click="getEqpForm('searchForm')"
        >{{ $t('CreatedMainForm') }}</el-button
        >
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { betterTips } from '@/utils/notification'
import createdManageApi from '@/api/maintenance/createdForm'
import { getFormItems } from '@/api/public'
export default {
  name: 'SearchForm',
  components: {},
  props: {},
  data() {
    return {
      formInline: {},
      rules: {
        pmId: [{ required: true, message: this.$t('pleaseSelectMaintenancePlan'), trigger: 'blur' }],
        formName: [{ required: true, message: this.$t('pleaseFillOutTheCheckForm'), trigger: 'blur' }],
        eqpId: [{ required: true, message: this.$t('pleaseFillInTheDeviceNumber'), trigger: 'blur' }]
      },
      PMFormList: [],
      eqpList: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.initPMFormList()
  },
  mounted() {},
  methods: {
    getEqpForm(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          const res = await getFormItems(this.formInline)
          if (res.code === 1) {
            const { toolingType, toolingModel } = res.data
            this.formInline.toolingType = toolingType
            this.formInline.toolingModel = toolingModel
            this.$emit('getEqpForm', { form: this.formInline })
          }
          betterTips(res)
        } else {
          return false
        }
      })
    },
    async getEqpList(val) {
      const res = await createdManageApi.getFormInfo({ pmId: val })
      // 防止用户手动更改
      this.formInline = Object.assign(this.formInline, res.data)
      this.eqpList = res.data.eqpList
    },
    // 检查表单
    async initPMFormList() {
      const res = await createdManageApi.getPMPlans()
      this.PMFormList = res.data
    }
  }
}
</script>

<style scoped lang="scss">
.el-form{
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    .el-form-item{
        width: 48%;
        .el-form-item__content div{
            width: 100%;
        }
    }
}
</style>
